package com.chat.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.chat.data.UserInfoDetItem;
import com.chat.util.ChatConstants;

public class InactiveUserCleanupThread extends Thread {

	@Override
	public void run() {
		while (true) {
			long lngCurrTime = (new Date()).getTime();
			List<UserInfoDetItem> lstUser = new ArrayList<UserInfoDetItem>(
					LoginActivity.lstUserDetInfo);

			for (int indexUsr = 0; indexUsr < LoginActivity.lstUserDetInfo
					.size(); indexUsr++) {
				UserInfoDetItem objUserInfoDetItem = LoginActivity.lstUserDetInfo
						.get(indexUsr);

				if ((lngCurrTime - objUserInfoDetItem.getLngLstKpAlvSgnl()) > ChatConstants.KEEP_ALIVE_MAX_WAIT) {
					System.out.println("Loggin out user "
							+ objUserInfoDetItem.getLngUserID()
							+ ". Last alive signal at "
							+ new Date(objUserInfoDetItem.getLngLstKpAlvSgnl())
									+ " and current time is "
									+ new Date(lngCurrTime));
					LogoutActivity.logoutUser(objUserInfoDetItem);
				}
			}

			try {
				Thread.sleep(ChatConstants.INACTIVE_USER_RFRSH_TIME_OUT);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
	}
}
